package cn.controller;

import cn.pojo.HealthUser;
import cn.service.HealthUserService;
import cn.utils.ImageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;

@CrossOrigin
@RestController
@RequestMapping(value = "user")
public class HealthUserController {


    @Autowired
    private HealthUserService userService;

    /**
     * 获取用户信息
     * @param openid 用户openid
     * @return 返回该用户信息
     */
    @GetMapping("/select")
    public Map<String, Object> selectUser(String openid) {
        System.out.println(openid);
        return userService.selectUser(openid);
    }

    /**
     * 添加用户
     * @param user 用户信息，传入用户
     * @return
     */
    @PostMapping("/add")
    public Map<String, Object> saveUser(HealthUser user) {
        System.out.println(user);
        return userService.saveUser(user);
    }

    @PostMapping("/update")
    public Map<String, Object> updateUser(HealthUser user) {
        return userService.updateUser(user);
    }

    /**
     * 上传身份证照片
     * @param idCard 身份证图片
     * @param openid 用户openid
     * @param front 是否是正面,1为正面，其他为反面
     * @return
     */
    @PostMapping("/file")
    public Map<String, Object> upload(@RequestParam(value = "idCard") MultipartFile file,
                                      @RequestParam(value = "openid") String openid,
                                      @RequestParam(value = "front") String isFront) {

        // 调用service将图片上传本地，并生成链接存入数据库用户信息，并识别图片获取用户数据
        return userService.uploadIdCard(file, openid, "1".equals(isFront));
    }

}
